package com.cloud.feeling.ststem.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.common.RestCode;
import com.cloud.common.RestResponse;
import com.cloud.common.model.User;
import com.cloud.common.model.UserToken;
import com.cloud.common.utils.PageQuery;
import com.cloud.common.utils.PageResponse;
import com.cloud.common.utils.RequestHolder;
import com.cloud.feeling.ststem.service.UserService;
import com.cloud.feeling.ststem.utils.JwtHelper;
import com.cloud.feeling.ststem.utils.RequestUtils;
import com.github.pagehelper.PageInfo;

/**
 * 
  * @Description: 用户管理
  * @author: feelj
  * @date: 2018年6月9日 上午9:36:14 
  *
 */
@Controller
@RequestMapping
public class UserController {
	
	@Autowired
	UserService userService;
	
	@GetMapping("/login")
	public String login() {
		RequestUtils.getRequest().setAttribute("target", RequestUtils.getRequest().getParameter("target"));
		return "login";
	}
	
	@RequestMapping("/index")
    public String index() {
    	return "index";
    }
	
	@GetMapping("/introduce")
    public String introduce() {
    	return "introduce/introduce";
    }
	
	@GetMapping("/main")
    public String main() {
    	return "main/main";
    }
	
	@GetMapping("/logout")
	@ResponseBody
	public RestResponse<UserToken> logout(String token) {
		UserToken logout = userService.logout(token);
        return RestResponse.success(logout);
    }
	
	@PostMapping("/login")
	@ResponseBody
	public RestResponse<UserToken> auth(User user){
		UserToken auth = userService.auth(user);
	    return RestResponse.success(auth);
	}
	
	@GetMapping("/personal")
	public String personal(Model model) {
		String checktoken = JwtHelper.Checktoken(RequestHolder.getRequest());
		User user = userService.getUserByToken(checktoken);
		model.addAttribute("user",user);
		return "/user/personal";
	}
	
	@PostMapping("/saveUser")
	@ResponseBody
	public RestResponse<RestCode> saveUser(User user){
		userService.saveUser(user);
		return RestResponse.success();
	}
	
	@GetMapping("/list")
	public String list() {
		return "/user/userlist";
	}
	
	@GetMapping("/userlist")
	@ResponseBody
	public PageResponse userlist(@RequestParam Map<String, Object> params){
		PageQuery query=new PageQuery(params);
		PageInfo<User> list = userService.list(query);
		PageResponse pageUtils = new PageResponse(list.getList(), list.getTotal());
		return pageUtils;
	}
	
	@GetMapping("/edituser")
	public String edituser(User user,Model model) {
		user = userService.select(user);
		model.addAttribute("user",user);
		return "/user/edituser";
	}
	
	@GetMapping("/adduser")
	public String adduser() {
		return "/user/adduser";
	}
	
	/***
	 * 校验用户名是否重复
	 * @return
	 */
	@GetMapping("verification")
	@ResponseBody
	public RestResponse<RestCode> verificationUsername(User user){
		return userService.verificationUsername(user);
	}
	
	@PostMapping("/deleteUser")
	@ResponseBody
	public RestResponse<RestCode> deleteUser(Integer[] ids){
		return userService.deleteUser(ids);
	}
	
}
